﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
using System;
using System.Linq;
using System.ComponentModel;
using System.Diagnostics;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Internal.VisualStudio.PlatformUI;

namespace PhotoStartPageControl
{
    class Utilities
    {
        /// <summary>
        /// Get the Visual Studio DTE object from the Start Page tool window DataContext.
        /// This allows controls to interact with Visual Studio through the DTE object model.
        /// </summary>
        /// <param name="dataContext">Start Page tool window DataContext</param>
        public static DTE2 GetDTE(object dataContext)
        {
            DataSource source = dataContext as DataSource;

            foreach (IPropertyDescription property in source.Properties)
            {
                if (property.Name == "DTE")
                {
                    return source.GetValue("DTE") as DTE2;
                }
            }

            return null;
        }

        /// <summary>
        /// Return the Visual Studio IServiceProvider interface.
        /// This allows controls to interact with Visual Studio through proffered services.
        /// </summary>
        /// <param name="dte">Visual Studio DTE object</param>
        public static ServiceProvider GetServiceProvider(DTE2 dte)
        {
            return new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte);
        }
    }
}
